package com.example.leetdemo.finash;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public class Solution515 {


    // 中序遍历


    public List<Integer> largestValues(TreeNode root) {

        List<Integer> result = new ArrayList<>();
        List<TreeNode> list = new ArrayList<>();
        if (root == null) {
            return result;
        }
        list.add(root);
        result.add(root.val);
        while (list.size() > 0) {
            List<TreeNode> temp = new ArrayList<>();
            int check = 0;
            int max = Integer.MIN_VALUE;
            for (TreeNode treeNode : list) {
                if (treeNode.left != null) {
                    check = 1;
                    temp.add(treeNode.left);
                    max = Math.max(max, treeNode.left.val);
                }
                if (treeNode.right != null) {
                    check = 1;
                    temp.add(treeNode.right);
                    max = Math.max(max, treeNode.right.val);
                }
            }
            if (check == 1) {
                result.add(max);
            }
            list = temp;
        }
        return result;

    }


    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public static void main(String[] args) {
        System.err.println(LocalDateTime.now().getMinute());
    }
}
